package cn.tedu.order.management.service.impl;

import cn.hutool.core.date.DateTime;
import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.order.management.dao.persist.repository.IGoodsRepository;
import cn.tedu.order.management.dao.persist.repository.IOrderItemRepository;
import cn.tedu.order.management.dao.persist.repository.IOrderRepository;
import cn.tedu.order.management.pojo.entity.Order;
import cn.tedu.order.management.pojo.entity.OrderItem;
import cn.tedu.order.management.pojo.param.OrderAddNewParam;
import cn.tedu.order.management.pojo.vo.GoodsStandardVO;
import cn.tedu.order.management.pojo.vo.OrderAdminListVO;
import cn.tedu.order.management.pojo.vo.OrderStaffListVO;
import cn.tedu.order.management.pojo.vo.UserOrderVO;
import cn.tedu.order.management.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderRepository iOrderRepository;

    @Autowired
    private IOrderItemRepository iOrderItemRepository;

    @Autowired
    private IGoodsRepository iGoodsRepository;

    @Override
    public Long create(CurrentPrincipal currentPrincipal, OrderAddNewParam orderAddNewParam) {
        //生成随机数
        Random random = new Random();

        long l = Math.abs(random.nextInt() + 10000L);

        /*新建订单对象*/
        Order order=new Order();
        Long goodsId=orderAddNewParam.getGoodsId();
        //GoodsStandardVO(id=null, goodsName=Goods 1, goodsPrice=100, title=Title 1, salesCount=10, gmtCreate=null, gmtModified=null)
        GoodsStandardVO goodsStandardVO=iGoodsRepository.getStandardById(goodsId);
        /*设置订单信息中各项信息*/

        //用户id
        order.setUserId(currentPrincipal.getId());
        //陪诊员id
        order.setStaffId(orderAddNewParam.getStaffId());
        //用户名称
        order.setUsername(currentPrincipal.getUsername());
        //商品id
        order.setGoodsId(goodsStandardVO.getId());
        //商品数量
        order.setGoodsNum(1);
        //订单编号
        order.setSerialNumber(l);
        //商品名称
        order.setProductName(goodsStandardVO.getGoodsName());
        //商品价格
        order.setPrice(goodsStandardVO.getGoodsPrice());
        //支付渠道
        if (orderAddNewParam.getPayChannel==null){
            order.setPayChannel(1);
        }
        order.setPayChannel(orderAddNewParam.getPayChannel);
        //订单状态
        order.setState(0);
        order.setGmtPay(LocalDateTime.now());
        //创建时间
        order.setGmtCreate( LocalDateTime.now());
        //修改时间
//        order.setGmtModified(LocalDateTime.now());
        BeanUtils.copyProperties(order,orderAddNewParam);

        iOrderRepository.addcnmd(order);


        OrderItem orderItem = new OrderItem();
        //订单id
        orderItem.setOrderId(l);
        //商品id
        orderItem.setGoodsId(goodsStandardVO.getId());
        //商品标题
        orderItem.setTitle(goodsStandardVO.getTitle());
        //商品价格
        orderItem.setUnitPrice(goodsStandardVO.getGoodsPrice());
        //商品数是
        orderItem.setNum(1);

        //创建时间
        orderItem.setGmtCreate( LocalDateTime.now());
        //修改时间
//        orderItem.setGmtModified(LocalDateTime.now());
        iOrderItemRepository.cnmdadd(orderItem);

        return order.getId();
    }

    @Override
    public void deleteById(Long id) {
        int rows = iOrderRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除订单失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Override
    public PageData<OrderAdminListVO> listForAdmin( Integer pageNum) {
        return iOrderRepository.listForAdmin(pageNum,defaultQueryPageSize);
    }

    @Override
    public PageData<OrderAdminListVO> listForAdmin(Integer pageNum, Integer pageSize) {
        return iOrderRepository.listForAdmin(pageNum,pageSize);
    }

    @Override
    public UserOrderVO listUserById(Long id) {
        return iOrderRepository.listUserById(id);
    }

    @Override
    public List<UserOrderVO> list(CurrentPrincipal currentPrincipal) {
        return iOrderRepository.list(currentPrincipal.getId());
    }

    @Override
    public void updateState(Long serialNumber, Integer state) {
        Order order=new Order();
        order.setSerialNumber(serialNumber);
        order.setState(state);
        iOrderRepository.updateState(order);
    }

    @Override
    public PageData<OrderStaffListVO> listForStaff(CurrentPrincipal currentPrincipal, Integer pageNum) {
        return iOrderRepository.listForStaff(currentPrincipal.getId(),pageNum,defaultQueryPageSize);
    }

    @Override
    public PageData<OrderStaffListVO> listForStaff(CurrentPrincipal currentPrincipal, Integer pageNum, Integer pageSize) {
        return iOrderRepository.listForStaff(currentPrincipal.getId(),pageNum,pageSize);
    }
}

